package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.merchant.coupon.entity.Coupon;
import com.hlf.modules.merchant.coupon.service.CouponService;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券接口
 */
@RestController
@RequestMapping("/api/coupon")
@Api("优惠券管理接口")
public class ApiCouponController extends AbstractController{

    @Autowired
    private CouponService couponService;
    @Autowired
    private StoreService storeService;


    /**
     * 新增优惠券
     */
    @SysLog("新增优惠券")
    @PostMapping("create")
    @ApiOperation(value = "新增优惠券",notes = "新增优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "spendMin", value = "最低消费金额", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "couponType", value = "优惠券类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "couponNum", value = "优惠额", required = true)
    })
    public ReturnResult create(@LoginUser SysUserEntity user,String couponType, String couponNum, String spendMin){
        Assert.isBlank(couponType, "优惠类型id不能为空");
        Assert.isBlank(couponNum, "优惠额不能为空");
        Assert.isBlank(spendMin, "最低消费金额不能为空");
        couponService.create(user,couponType,couponNum,spendMin);
        return ReturnResult.ok();
    }


    /**
     * 打开关闭优惠券使用开关
     */
    @SysLog("打开/关闭优惠券开关")
    @PostMapping("open")
    @ApiOperation(value = "打开关闭优惠券使用开关", notes = "打开关闭优惠券使用开关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "storeCouponStatusStr", value = "优惠券开关  1 : 使用 , 2 : 不使用',", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    @RequiresRoles(value = {"merchant", "storeAdmin"}, logical = Logical.OR)
    public ReturnResult open(@LoginUser SysUserEntity user, String storeCouponStatusStr) {
        couponService.open(user, storeCouponStatusStr);
        return ReturnResult.ok();
    }


    /**
     * 删除优惠券
     */
    @SysLog("删除优惠券")
    @PostMapping("delete")
    @ApiOperation(value = "删除优惠券",notes = "删除优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType="string", name = "id", value = "优惠券id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult delete(String id,HttpServletRequest request){
        Assert.isBlank(id, "优惠券id不能为空");
        couponService.delete(id,request);
        return ReturnResult.ok();
    }


    /**
     * 优惠券列表
     */
    @PostMapping("list")
    @ApiOperation(value = "优惠券列表",notes = "优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult list(@LoginUser SysUserEntity user){
        List<Coupon> list = couponService.list(user.getCurrentStoreId());
        String couponName;
        for (Coupon coupon : list) {
            Integer couponNum = coupon.getCouponNum();
            if (coupon.getCouponType() == Constants.STORE_COUPON_TYPE_REBATE){
                //整单折扣
                couponName = "整单" + new DecimalFormat("0.0").format(couponNum/10.0) + " 折";
            }else {
                //整单减免
                couponName = "整单减免" + new DecimalFormat("0.00").format(couponNum/100.0) + "元";
            }
            coupon.setName(couponName);
        }
        Store store = storeService.queryObject(Long.parseLong(user.getCurrentStoreId()));
        Byte storeCouponStatus = store.getStoreCouponStatus();
        Map<String, Object> map = new HashMap<>();
        map.put("storeCouponStatus",storeCouponStatus);
        map.put("list",list);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }
}
